<style scoped lang="less">
.filterBox {
  max-width: 800px;
  margin: 0 auto;
  display:flex;
  flex-flow: row nowrap;
  justify-content: space-between;
  align-items: center;
  &__button{
    width: 86px;
    height: 25px;
    border-radius: 20px;
    border: 1px solid rgba(153, 153, 153, 0.53);
    text-align: center;
    line-height: 25px;
    padding: 0 14px;
    font-size: 14px;
    color: #6f6f6f;
    cursor: pointer;
    transition: all ease-in-out .3s;
    &:hover{ color: rgba(189, 1, 1, 0.62);border-color: rgba(189, 1, 1, 0.57)
    }
  }
  &__popup{
    width: 640px;
    background: #ffffff;
    border-radius: 10px;
    &__top{
      width: 100%;
      box-sizing: border-box;
      padding: 20px;
      border-bottom: 1px solid #eee;
      &__button{
        color: rgba(20, 27, 39, 0.7);
        font-size: 14px;
        cursor: pointer;
        width: max-content;
        padding: 0 12px;
        border-radius: 10px;
        line-height: 25px;
        height: 25px;
        transition: all ease-in-out .2s;
        &:hover{
          color: #fc6565;
        }
        &-active{
          color: #fc6565;
          background: rgba(255, 212, 212, 0.48);
        }
      }
    }
    &__grid{
      box-sizing: border-box;
      padding: 10px 20px;
      &__row{
        display: flex;
        flex-flow: row nowrap;
        align-items: flex-start;
        margin-bottom: 20px;
        &__pre{
          display: flex;
          flex-flow: row nowrap;
          align-items: flex-start;
          margin-right: 40px;
          &__icon{
            width: 20px;
            height: 20px;
            margin-right: 10px;
          }
          &__label{
            font-size: 14px;
            font-weight: 400;
            color: #b1b1b1;
          }
        }
        &__list{
          padding-top: 1px;
          flex: 1;
          display: grid;
          grid-template-columns: repeat(6,18%);
          &__item{
            margin-right: 10px;
            height: 30px;
            cursor: pointer;
            &-active > &__text{
              color: red;
            }
            &__text{
              font-size: 12px;
              color: #4e4e4e;
              &:hover{
                color: red;
              }
            }
          }
        }
      }
    }
  }

  &__tags{
    display: flex;
    flex-flow: row nowrap;
    &__item{
      display: flex;
      flex-flow: row nowrap;
      height: 12px;
      &:hover{ cursor: pointer;}
      &:hover > &__text { color: rgba(189, 1, 1, 0.62); }
      &__text{
        transition: all ease-in-out .3s;
        font-size: 12px;
        font-weight: 400;
        color: #999999;
        line-height: 14px;
        &-active{
          color: red;
        }
      }
      &__divider{
        height: 100%;
        width: 1px;
        margin: 0 8px;
        background: #d7d7d9;
      }
    }
  }
}
</style>
<template>
  <div class="filterBox">
    <Popup v-model="showPopUp" :offsetY="-20" :offsetX="0" :zIndex="2" position="bottom" style="display: flex;align-items: center">
      <template v-slot:ref><div class="filterBox__button">{{ !props.value ? '全部歌单' : props.value }}</div></template>
      <div class="filterBox__popup">
        <div class="filterBox__popup__top">
          <div @click="handleClickItem({name:null})" class="filterBox__popup__top__button" :class="{'filterBox__popup__top__button-active': !props.value}">全部歌单</div>
        </div>
        <div class="filterBox__popup__grid">
          <div class="filterBox__popup__grid__row" v-for="(ele,index) of categoryList">
            <div class="filterBox__popup__grid__row__pre">
              <img :src="ele?.icon" class="filterBox__popup__grid__row__pre__icon">
              <div class="filterBox__popup__grid__row__pre__label">{{ ele?.name }}</div>
            </div>
            <div class="filterBox__popup__grid__row__list">
              <div class="filterBox__popup__grid__row__list__item" :class="{'filterBox__popup__grid__row__list__item-active':props.value === item?.name}" @click="handleClickItem(item)" v-for="(item,index) of ele?.children">
                <div class="filterBox__popup__grid__row__list__item__text">{{ item.name }}</div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </Popup>
    <div class="filterBox__tags">
      <div class="filterBox__tags__item" v-for="(ele,index) of tagsList">
        <div class="filterBox__tags__item__divider" v-if="index !== 0"/>
        <div class="filterBox__tags__item__text" @click="emits('update:value',ele?.name)" :class="{ 'filterBox__tags__item__text-active': props.value === ele?.name }">{{ ele.name }}</div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
// todo 资源
//@ts-ignore
import cafe from '../../icon/cafe.png'
//@ts-ignore
import earth from '../../icon/earth.png'
//@ts-ignore
import music from '../../icon/music.png'
//@ts-ignore
import squat from '../../icon/squat.png'
//@ts-ignore
import smile from '../../icon/smile.png'
import {computed, ref, watch} from "vue";
const iconList = [earth,music,cafe,smile,squat]
import Popup from '@/components/popUp'
import {AxiosService} from "@/core";
const axios = new AxiosService('netEasyMusic')
const props = defineProps({
  value:{ type: [String,Number], default: null },
  el:{ type:HTMLElement, default: null }
})
const emits = defineEmits(['update:value'])
// todo 数据
// 歌单类型所有数据
const categoryListApi = ref({})
const categoryList = computed(() => {
  const arr = []
  for (let categoriesKey in categoryListApi?.value?.categories) {
    arr.push({
      name: categoryListApi?.value?.categories?.[categoriesKey],
      icon: iconList[categoriesKey],
      children: categoryListApi?.value?.sub?.filter( (ele:any) => String(ele?.category) === String(categoriesKey) )
    })
  }
  return arr
})
const tagsListApi = ref([])
const tagsList = computed(() => (tagsListApi.value?.map((ele:any) => ({
  name: ele?.name,
  value: ele?.id,
}))))
// 显示更多选项
const showPopUp = ref(false)

// todo 请求
// 常用列表
axios.get({
  api:'/playlist/hot',
  success: ({tags}) => tagsListApi.value = tags
})

// 全部歌单类型
axios.get({
  api:'/playlist/catlist',
  success: (res:any) => categoryListApi.value = res
})
// 交互
const handleClickItem = (event:any) => {
  props.el && props.el?.scrollTo({top:0,behavior:'smooth'})
  const { name } = event
  emits('update:value',name)
  showPopUp.value = false
}
</script>

